package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @projectName: sky-take-out
 * @package: com.sky.service.impl
 * @className: ShoppingCartServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/10/5 9:21
 * @version: 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 判断是菜还是套餐
        if (Objects.nonNull(shoppingCartDTO.getDishId())){
            // 加菜
            Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
            // 根据用户id,菜品Id,口味  去查重 - 实体
            ShoppingCart shoppingCartQuery = new ShoppingCart();
            shoppingCartQuery.setUserId(BaseContext.getCurrentId());
            shoppingCartQuery.setDishId(shoppingCartDTO.getDishId());
            shoppingCartQuery.setDishFlavor(shoppingCartDTO.getDishFlavor());
            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartQuery);
            if (Objects.nonNull(shoppingCart)){
                // 已经加购过了,我们只需要更新数量
                shoppingCart.setNumber(shoppingCart.getNumber()+1);
                shoppingCartMapper.update(shoppingCart);
            }else{
                // 如果是第一次加入购物车,则新增
                shoppingCart = new ShoppingCart();
                shoppingCart.setName(dish.getName());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }
        }
        if (Objects.nonNull(shoppingCartDTO.getSetmealId())){
            SetmealVO setmeal = setmealMapper.getSetmeal(shoppingCartDTO.getSetmealId());
            // 根据用户id,套餐id
            ShoppingCart shoppingCartQuery = new ShoppingCart();
            shoppingCartQuery.setUserId(BaseContext.getCurrentId());
            shoppingCartQuery.setSetmealId(shoppingCartDTO.getSetmealId());
            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(shoppingCartQuery);
            if (Objects.nonNull(shoppingCart)){
                shoppingCart.setNumber(shoppingCart.getNumber()+1);
                shoppingCartMapper.update(shoppingCart);
            }else{
                shoppingCart = new ShoppingCart();
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }
        }
    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCartQuery = new ShoppingCart();
        shoppingCartQuery.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCartQuery);
        return shoppingCartList;
    }

    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }
}
